package ext.acae2.baseline.web;

import java.util.ArrayList;
import java.util.List;

import org.apache.log4j.Logger;

import wt.doc.WTDocument;
import wt.epm.EPMDocument;
import wt.fc.PersistenceHelper;
import wt.fc.QueryResult;
import wt.log4j.LogR;
import wt.method.RemoteAccess;
import wt.part.WTPart;
import wt.util.WTException;
import wt.vc.baseline.ManagedBaseline;

public class BaselineUtil implements RemoteAccess{

    private static final Logger LOGGER = LogR.getLogger(BaselineUtil.class.getName());
	/**
     * 得到基线中的文件及部件
     * @param baseline
     * @param baselineDocs
     * @param baselineEpmDocs
     * @param baselineParts
     * @throws WTException
     */
	@SuppressWarnings("rawtypes")
	public static void getBaselineDocsAndParts(ManagedBaseline baseline, List<WTDocument> baselineDocs, List<WTPart> baselineParts, List<EPMDocument> baselineEpmDocs)  throws WTException{
    	QueryResult qr = PersistenceHelper.manager.navigate(baseline, "theBaselineable", wt.vc.baseline.BaselineMember.class, true);
    	List listQR = qr.getObjectVectorIfc().getVector();
    	for (Object baselineObj: listQR) {
    		if(baselineObj instanceof WTDocument){
    			baselineDocs.add((WTDocument)baselineObj);
    		}else if(baselineObj instanceof WTPart){
    			baselineParts.add((WTPart)baselineObj);
    		}else if(baselineObj instanceof EPMDocument){
    			baselineEpmDocs.add((EPMDocument)baselineObj);
    		}
    	}
    }

    public static List<WTPart> getBaselineParts(ManagedBaseline baseline) throws WTException {
        LOGGER.debug("> Start getBaselineParts");
        
        QueryResult result = PersistenceHelper.manager.navigate(
                baseline, "theBaselineable", wt.vc.baseline.BaselineMember.class, true);
        
        List<WTPart> partList = new ArrayList<WTPart>();
        while (result.hasMoreElements()) {
            Object baselineObj = result.nextElement();
            if (baselineObj instanceof WTPart) {
                partList.add((WTPart) baselineObj);
            }
        }
        
        LOGGER.debug("> End getBaselineParts");
        return partList;
    }
    
    public static List<WTDocument> getBaselineDocuments(ManagedBaseline baseline) throws WTException {
        LOGGER.debug("> Start getBaselineDocuments");
        
        QueryResult result = PersistenceHelper.manager.navigate(
                baseline, "theBaselineable", wt.vc.baseline.BaselineMember.class, true);
        
        List<WTDocument> docList = new ArrayList<WTDocument>();
        while (result.hasMoreElements()) {
            Object baselineObj = result.nextElement();
            if (baselineObj instanceof WTDocument) {
                docList.add((WTDocument) baselineObj);
            }
        }
        
        LOGGER.debug("> End getBaselineDocuments");
        return docList;
    }
    
    public static List<EPMDocument> getBaselineEPM(ManagedBaseline baseline) throws WTException {
        LOGGER.debug("> Start getBaselineEPM");
        
        QueryResult result = PersistenceHelper.manager.navigate(
                baseline, "theBaselineable", wt.vc.baseline.BaselineMember.class, true);
        
        List<EPMDocument> epmList = new ArrayList<EPMDocument>();
        while (result.hasMoreElements()) {
            Object baselineObj = result.nextElement();
            if (baselineObj instanceof EPMDocument) {
                epmList.add((EPMDocument) baselineObj);
            }
        }
        
        LOGGER.debug("> End getBaselineEPM");
        return epmList;
    }

    public static List<Object> getBaselineObjects(ManagedBaseline baseline) throws WTException {
        LOGGER.debug("> Start getBaselineDocuments");
        
        QueryResult result = PersistenceHelper.manager.navigate(
                baseline, "theBaselineable", wt.vc.baseline.BaselineMember.class, true);
        
        List<WTDocument> docList = new ArrayList<WTDocument>();
        List<WTPart> partList = new ArrayList<WTPart>();
        List<EPMDocument> epmList = new ArrayList<EPMDocument>();
        
        while (result.hasMoreElements()) {
            Object baselineObj = result.nextElement();
            if (baselineObj instanceof WTDocument) {
                docList.add((WTDocument) baselineObj);
            }
            else if (baselineObj instanceof WTPart) {
                partList.add((WTPart) baselineObj);
            }
            else if (baselineObj instanceof EPMDocument) {
                epmList.add((EPMDocument) baselineObj);
            }
        }
        
        List<Object> resultList = new ArrayList<Object>();
        resultList.addAll(partList);
        resultList.addAll(epmList);
        resultList.addAll(docList);
        
        LOGGER.debug("> End getBaselineDocuments");
        return resultList;
    }
}
